package com.ljkj.zjf.rest;

import java.math.BigDecimal;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;

import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ljkj.access.Tright;
import com.ljkj.access.Tuser;
import com.ljkj.common.application.SpringApplicationContext;
import com.ljkj.common.db.domain.ZjfGoodsActivity;
import com.ljkj.common.db.domain.ZjfGoodsType;
import com.ljkj.common.db.util.DateTime;
import com.ljkj.common.db.util.JSONUTIL;
import com.ljkj.zjf.common.CacheManager;
import com.ljkj.zjf.common.GlobalNames;
import com.ljkj.zjf.service.ActivityRecordService;
import com.ljkj.zjf.service.ActivityService;
import com.ljkj.zjf.service.TRightService;
import com.ljkj.zjf.service.TuserRightService;
import com.ljkj.zjf.service.TuserService;

@Path("rest/activity")
public class ActivitySource {
	
	private static ActivityService activityService;
	private static TuserService tuserService;
	private static ActivityRecordService activityRecordService;
	
	//初始化service
	static {
		activityService  = (ActivityService) SpringApplicationContext.getBean("activityService");
		tuserService  = (TuserService) SpringApplicationContext.getBean("tuserService");
		activityRecordService = (ActivityRecordService) SpringApplicationContext.getBean("activityRecordService");
	}
	
	private static Logger logger = LoggerFactory.getLogger(ActivitySource.class);
	
	@Path("/list")
	@POST
	@Produces("application/json; charset=utf-8")
	public String goodsActivityList(String json_para) throws Exception {
		
		String decode_str = URLDecoder.decode(json_para, "utf-8");
		decode_str = JSONUTIL.JsonFromString(decode_str);
		JSONObject json_object = JSON.parseObject(decode_str);
		String userId = json_object.getString("userId"); 
		
		Map<String, Object> errorMap = new HashMap<String, Object>();
		errorMap.put("errCode", GlobalNames.CALL_RESULT_FAIL);
		
		if (userId == null || "".equals(userId)) {
			errorMap.put("errMsg", "用户名不能为空");
			return JSON.toJSONString(errorMap);
		}
		
		String matchRes = CacheManager.getSession().getIfPresent(userId);
		if (null == matchRes || "".equals(matchRes)) {
			errorMap.put("errMsg", "您尚未登录，请登录再操作。");
			return JSON.toJSONString(errorMap);
		}
		
		String params = json_object.getString("params");
		String pageIndex = json_object.getString("pageIndex");
		int currentPage = 1;
		if(pageIndex != null && !"".equals(pageIndex)){
			currentPage = Integer.parseInt(pageIndex)+1;
		}
		String pageSize_str = json_object.getString("pageSize");
		int pageSize = 10;
		if(pageSize_str != null && !"".equals(pageSize_str)){
			pageSize = Integer.parseInt(pageSize_str);
		}
		String sortField = json_object.getString("sortField");
		String sortOrder = json_object.getString("sortOrder");
		
		String result = this.activityService.getActivitysByProperties(params, currentPage, pageSize, sortField, sortOrder);
		return result;
		
	}
	
	@Path("/list_for_good")
	@GET
	@Produces("application/json; charset=utf-8")
	public String activityListForGood(String json_para) throws Exception {
		
		String result = this.activityService.getActivityFroGood();
		return result;
	}
	
	
	@Path("/info")
	@POST
	@Produces("application/json; charset=utf-8")
	public String addOrEditActivityInfo(String json_para) throws Exception {
		
		String decode_str = URLDecoder.decode(json_para, "utf-8");
		decode_str = JSONUTIL.JsonFromString(decode_str);
		JSONObject json_object = JSON.parseObject(decode_str);
		
		Map<String, Object> errorMap = new HashMap<String, Object>();
		errorMap.put("errCode", GlobalNames.CALL_RESULT_FAIL);
		String userId = json_object.getString("userId"); 
		if (userId == null || "".equals(userId)) {
			errorMap.put("errMsg", "用户名不能为空");
			return JSON.toJSONString(errorMap);
		}
		
		String activityId = json_object.getString("activityId");
		ZjfGoodsActivity zjfGoodsActivity = null;
		if(activityId != null && !"".equals(activityId)){
			zjfGoodsActivity = this.activityService.getActivityById(activityId);
		}
		if(zjfGoodsActivity == null){
			zjfGoodsActivity = new ZjfGoodsActivity();
		}
		
		String activityName = json_object.getString("activityName");
		if(activityName!= null && !"".equals(activityName)){
			zjfGoodsActivity.setActivityName(activityName);
		}
		String activityPrice_temp = json_object.getString("activityPrice");
		BigDecimal activityPrice;
		if(activityPrice_temp != null && !"".equals(activityPrice_temp)){
			activityPrice = new BigDecimal(activityPrice_temp);
			zjfGoodsActivity.setActivityPrice(activityPrice);
		}
		String discount = json_object.getString("discount");
		if(discount != null && !"".equals(discount)){
			zjfGoodsActivity.setDiscount(discount);
		}
		
		String activityBeginTime = json_object.getString("activityBeginTime");
		if(activityBeginTime!= null && !"".equals(activityBeginTime)){
			zjfGoodsActivity.setActivityBeginTime(DateTime.format2DateTime(activityBeginTime));
		}
		
		String activityEndTime = json_object.getString("activityEndTime");
		if(activityEndTime!= null && !"".equals(activityEndTime)){
			zjfGoodsActivity.setActivityEndTime(DateTime.format2DateTime(activityEndTime));
		}
		
		String activityContent = json_object.getString("activityContent");
		if(activityContent!= null && !"".equals(activityContent)){
			zjfGoodsActivity.setActivityContent(activityContent);
		}
		
		
		Integer disabledState = null;
		String disabledState_temp = json_object.getString("disabledState");
		if(disabledState_temp != null && !"".equals(disabledState_temp)){
			disabledState = Integer.parseInt(disabledState_temp);
			zjfGoodsActivity.setDisabledState(disabledState);
		}
		
		Tuser tuser = this.tuserService.getUserById(Integer.valueOf(userId));
		
		String result = this.activityService.addOrEditActivityInfo(zjfGoodsActivity, tuser);
		return result;
		
	}
	
	@Path("/list_from_record")
	@POST
	@Produces("application/json; charset=utf-8")
	public String activityListFromRecord(String json_para) throws Exception {
		
		String decode_str = URLDecoder.decode(json_para, "utf-8");
		decode_str = JSONUTIL.JsonFromString(decode_str);
		JSONObject json_object = JSON.parseObject(decode_str);
		
		String params = json_object.getString("params");
		String pageIndex = json_object.getString("pageIndex");
		int currentPage = 1;
		if(pageIndex != null && !"".equals(pageIndex)){
			currentPage = Integer.parseInt(pageIndex)+1;
		}
		String pageSize_str = json_object.getString("pageSize");
		int pageSize = 10;
		if(pageSize_str != null && !"".equals(pageSize_str)){
			pageSize = Integer.parseInt(pageSize_str);
		}
		String activityId = json_object.getString("activityId");
		String sortField = json_object.getString("sortField");
		String sortOrder = json_object.getString("sortOrder");
		
		String result = this.activityRecordService.getActivityRecordByProperties(params, activityId, currentPage, pageSize, sortField, sortOrder);
		return result;
	}

}
